/*
 * Copyright (c) 2012 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include "mem.h"


int main(int argc, char *argv[])
{
	if((argc != 4) || (strchr(argv[1], 'h') != NULL)) //-h -help ...
	{
		printf("1:  3 arguments: start_addr  total_lenth  row_lenth(unsigned int type times);\n");
		printf("2:  if fpga mmap to ps, addr must for 4 times ;\n");
		printf("3:  Example: ./print_mem  0x41210000  0x1000  16 \n");
		printf("4:  addr from L to H , follow char* one Byte printf \n");
		return 0;
	}

    char *p=NULL, *q=NULL;
    unsigned int addr = strtoul(argv[1], &p, 16);
    unsigned int len  = strtoul(argv[2], &q, 16);
    int idex  = atoi(argv[3]);

    if((len==0)||(idex <= 0)||(p[0] != '\0')||(q[0] != '\0'))
    {
    	printf("arguments err: %s  %s  %s\n", argv[1], argv[2], argv[3]);
    	return 0;
    }

    unsigned int * vir = addr_mmap(addr, len);
    if(vir == 0)
    {
    	printf("mem failed \n");
    	return 0;
    }

    for(unsigned int i=0; i < len ; i++)
    {
    	if(i % idex == 0) {
    		printf("\n");
    		printf("0x%08X  ", addr + i*4);
    	}
    	printf("%08X ", *vir++);
    }
    printf("\n");
    return 0;
}



//int main(int argc, char *argv[])
//{
//	if((argc != 4) || (strchr(argv[1], 'h') != NULL)) //-h -help ...
//	{
//		printf("1:  3 arguments: start_addr  total_lenth  row_lenth(unsigned int type times);\n");
//		printf("2:  if fpga mmap to ps, addr must for 4 times ;\n");
//		printf("3:  Example: ./print_mem  0x41210000  0x1000  16 \n");
//		printf("4:  addr from L to H , follow char* one Byte printf \n");
//		return 0;
//	}
//
//    char *p=NULL, *q=NULL;
//    unsigned int addr = strtoul(argv[1], &p, 16);
//    unsigned int len  = strtoul(argv[2], &q, 16);
//    int idex  = atoi(argv[3]);
//
//    if((len==0)||(idex <= 0)||(p[0] != '\0')||(q[0] != '\0'))
//    {
//    	printf("arguments err: %s  %s  %s\n", argv[1], argv[2], argv[3]);
//    	return 0;
//    }
//
//    unsigned char * vir = addr_mmap(addr, len);
//    if(vir == 0)
//    {
//    	printf("mem failed \n");
//    	return 0;
//    }
//
//    for(unsigned int i=0; i*4+3 < len; i++)
//    {
//    	if(i % idex == 0) {
//    		printf("\n");
//    		printf("0x%08X  ", addr+i*4);
//    	}
//    	printf("%02X%02X%02X%02X ", *(vir+i*4+0), *(vir+i*4+1), *(vir+i*4+2), *(vir+i*4+3));
//    }
//    printf("\n");
//    return 0;
//}
